package com.jt.aop;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.vo.SysResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class SystemException {

    @ExceptionHandler({RuntimeException.class})
    public Object fail(Exception e, HttpServletRequest request){
        log.error(e.getLocalizedMessage(),e);
        e.printStackTrace();
        //获取callback请求参数
        String callback = request.getParameter("callback");
        //判断是否为jsonp跨域请求
        if(StringUtils.hasLength(callback)){//如果有值则返回callback(JSON)
            JSONPObject jsonpObject=new JSONPObject(callback, SysResult.fail());
            return jsonpObject;
        }
        return SysResult.fail();
    }
}
